﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace PreContractWeb.Administration
{
    public partial class RoleMenuMapping : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {

                BindMenuTree();

            }
        }

        /// <summary>
        /// Setting the tree view control property.
        /// </summary>
        /// <param name="value">tree node's value</param>
        /// <param name="text">tree node's text</param>
        /// <param name="showcheckBox">tree node's checkbox control property</param>
        /// <param name="nodeChecked">tree node's checkbox has been check or not</param>
        /// <returns></returns>
        private TreeNode NewNodeSetting(string value, string text, bool showcheckBox, bool nodeChecked)
        {
            TreeNode newNode = new TreeNode();
            newNode.SelectAction = TreeNodeSelectAction.None;
            newNode.ShowCheckBox = showcheckBox;
            newNode.Checked = nodeChecked;
            newNode.Value = value;
            newNode.Text = text;
            return newNode;
        }

        /// <summary>
        /// Bind data to tree view control.
        /// </summary>
        void BindMenuTree()
        {

            
            BEGDC.PreContract.BLL.IMenuConfig menulist = (BEGDC.PreContract.BLL.IMenuConfig)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_MenuConfig_Service];
            
            DataTable dt = menulist.GetMenuConfig_DataTable(" ISENABLED = '1' ");


            TreeNode root = NewNodeSetting("root", "主菜单", false, false);


            menuTree.Nodes.Add(root);

            if (dt != null)
            {
                menuTree.ExpandDepth = 3;

                MenuTreeBind(dt, "0", root);
            }


        }


        /// <summary>
        /// Bind data to tree view control.
        /// </summary>
        /// <param name="dtlist">the data source of tree view control</param>
        /// <param name="parentid">parent node id</param>
        /// <param name="parentNode">parent node</param>
        private void MenuTreeBind(DataTable dtlist, string parentid, TreeNode parentNode)
        {

            DataRow[] replist = dtlist.Select("ParentMenuID = '" + parentid + "'", "MenuSequenceID");

            foreach (DataRow rep in replist)
            {
                string menu_ID = rep["MenuID"].ToString().Trim();

                string menu_NAME = rep["MenuTitle"].ToString().Trim();

                TreeNode childnode = childnode = NewNodeSetting(menu_ID, menu_NAME, false, false);

                parentNode.ChildNodes.Add(childnode);

                if (childnode != null && childnode.Depth == 2)
                {
                    childnode.Expanded = false;

                    BEGDC.PreContract.BLL.IMenuRoleMapping menulist = (BEGDC.PreContract.BLL.IMenuRoleMapping)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_MenuRoleMapping_Service];

                    BEGDC.PreContract.BLL.IRole roles = (BEGDC.PreContract.BLL.IRole)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_Role_Service];

                    DataTable dtuserrole = menulist.GetMenuRoleByMenuId(menu_ID).Tables[0];

                    DataTable dtrole = roles.GetRole_DataTable(string.Empty);

                    for (int i = 0; i < dtrole.Rows.Count; i++)
                    {
                        bool tag = false;

                        string roleid = dtrole.Rows[i][BEGDC.PreContract.DAL.Role.CONST_RoleID].ToString().Trim();

                        string rolename = dtrole.Rows[i][BEGDC.PreContract.DAL.Role.CONST_RoleName].ToString().Trim();

                        for (int j = 0; j < dtuserrole.Rows.Count; j++)
                        {
                            string uroleid = dtuserrole.Rows[j][BEGDC.PreContract.DAL.Role.CONST_RoleID].ToString().Trim();
                            if (roleid == uroleid)
                            {
                                tag = true;
                                break;
                            }
                        }

                        TreeNode child2node = null;

                        if (tag)
                        {
                            child2node = NewNodeSetting(roleid, rolename, true, true);

                        }
                        else
                        {
                            child2node = NewNodeSetting(roleid, rolename, true, false);
                        }

                        childnode.ChildNodes.Add(child2node);
                    }



                }

                MenuTreeBind(dtlist, menu_ID, childnode);
            }


        }
        protected void btnsave_Click(object sender, EventArgs e)
        {
            TreeNode parentnode = this.menuTree.Nodes[0];



            if (parentnode.Value == "root")
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("RoleID");
                dt.Columns.Add("MenuID");
                for (int i = 0; i < parentnode.ChildNodes.Count; i++)
                {
                    TreeNode childnode = parentnode.ChildNodes[i];

                    string childvalue = childnode.Value;

                    //bool flag = true;

                    if (childnode.ChildNodes.Count != 0)
                    {

                        for (int j = 0; j < childnode.ChildNodes.Count; j++)
                        {
                            TreeNode child2node = childnode.ChildNodes[j];

                            for (int k = 0; k < child2node.ChildNodes.Count; k++)
                            {

                                if (child2node.ChildNodes[k].Checked)
                                {
                                    //if (flag)
                                    //{
                                    DataRow drparent = dt.NewRow();
                                    drparent[0] = child2node.ChildNodes[k].Value;
                                    drparent[1] = childvalue;
                                    //flag = false;
                                    dt.Rows.Add(drparent);
                                    //}
                                    //else
                                    //{
                                    DataRow drchild = dt.NewRow();
                                    drchild[0] = child2node.ChildNodes[k].Value;
                                    drchild[1] = child2node.Value;
                                    dt.Rows.Add(drchild);
                                    //}



                                }
                            }


                        }

                    }



                }
                if (dt.Rows.Count != 0)
                {
                    BEGDC.PreContract.BLL.IMenuRoleMapping rolelist = (BEGDC.PreContract.BLL.IMenuRoleMapping)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_MenuRoleMapping_Service];
                    rolelist.AddMenuRole(dt);
                }
            }
        }
    }
}
